<template>
  <div>
    <el-dialog
      title="月抄表统计查看页面"
      :visible.sync="dialogVisibleupdate"
      width="20%"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForms"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="楼栋：" :label-width="formLabelWidth">
          {{ ruleForms.houseNum }}
        </el-form-item>
        <el-form-item label="楼层：" :label-width="formLabelWidth">
          {{ ruleForms.floor }}
        </el-form-item>
        <el-form-item label="房间：" :label-width="formLabelWidth">
          {{ ruleForms.rooms }}
        </el-form-item>
        <el-form-item label="设备类型：" :label-width="formLabelWidth">
          {{ ruleForms.typeName }}
        </el-form-item>
        <el-form-item label="设备编号：" :label-width="formLabelWidth">
          {{ ruleForms.typeCoding }}
        </el-form-item>
        <el-form-item label="设备名称：" :label-width="formLabelWidth">
          {{ ruleForms.equipmentNumber }}
        </el-form-item>
        <el-form-item label="抄表月份：" :label-width="formLabelWidth">
          {{ ruleForms.month }}
        </el-form-item>
        <el-form-item label="起码：" :label-width="formLabelWidth">
          {{ ruleForms.startNum }}
        </el-form-item>
        <el-form-item label="止码：" :label-width="formLabelWidth">
          {{ ruleForms.endNum }}
        </el-form-item>
        <el-form-item label="差额：" :label-width="formLabelWidth">
          {{ ruleForms.nums }}
        </el-form-item>
        <el-form-item label="月用量：" :label-width="formLabelWidth">
          {{ ruleForms.nums }}
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-form
      :inline="true"
      class="demo-form-inline"
      style="text-align: left"
      :model="ruleFormData"
    >
      <el-form-item>
        <el-button type="primary" @click="exportToExcel">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="序号" width="180"> </el-table-column>
      <el-table-column prop="houseNum" label="楼栋" width="180">
      </el-table-column>
      <el-table-column prop="floor" label="楼层" width="180"> </el-table-column>
      <el-table-column prop="rooms" label="房间" width="180"> </el-table-column>
      <el-table-column prop="typeName" label="设备类型" width="180">
      </el-table-column>
      <el-table-column prop="typeCoding" label="设备编号" width="180">
      </el-table-column>
      <el-table-column prop="equipmentNumber" label="设备名称" width="180">
      </el-table-column>
      <el-table-column
        prop="month"
        label="抄表月份"
        width="180"
        :formatter="formatDate"
      >
      </el-table-column>
      <el-table-column prop="startNum" label="起码" width="180">
      </el-table-column>
      <el-table-column prop="endNum" label="止码" width="180">
      </el-table-column>
      <el-table-column label="差额" width="180">
        <template slot-scope="scope">
          {{ scope.row.endNum - scope.row.startNum }}
        </template>
      </el-table-column>
      <el-table-column label="月用量" width="180">
        <template slot-scope="scope">
          {{ scope.row.endNum - scope.row.startNum }}
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="180">
        <el-link type="primary" @click="updatedata">查看</el-link>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="ruleFormData.pageIndex"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="ruleFormData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="count"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  GetCount,
  GetMonthStatisticssPage,
} from "@/api/assetManagecopy/MonthStatistics";

export default {
  data() {
    return {
      ruleForms: {},
      dialogVisibleupdate: false,
      ruleFormData: {
        pageIndex: 1,
        pageSize: 6,
      },
      count: 0,
      countnum: 0,
      //显示的数据
      tableData: [],
      multipleSelection: [],
      ids: [],
    };
  },
  created() {
    this.ListData();
    this.totalcount();
  },
  methods: {
    //获取总条数方法
    totalcount() {
      GetCount().then((res) => {
        this.countnum = res.data;
      });
    },
    totalcount() {
      GetTotaocount().then((res) => {
        this.countnum = res.data;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.ruleFormData.pageSize = val;
      this.ListData();
    },
    handleCurrentChange(val) {
      this.ruleFormData.pageIndex = val;
      this.ListData();
    },
    //显示方法
    ListData() {
      GetMonthStatisticssPage(this.ruleFormData).then((res) => {
        // this.tableData = res.data.data

        res.data.data.forEach((models) => {
          var pushdata = models.endNum - models.startNum;
          models.nums = pushdata;
          this.tableData.push(models);
        });
        this.count = res.data.totalCount;
      });
    },
    //表格显示具体时间
    formatDate(row, column) {
      // 获取单元格数据
      let data = row[column.property];
      if (data == null) {
        return 1;
      }
      let dt = new Date(data);
      return (
        dt.getFullYear() +
        "-" +
        (dt.getMonth() + 1) +
        "-" +
        dt.getDate() +
        " " +
        dt.getHours() +
        ":" +
        dt.getMinutes() +
        ":" +
        dt.getSeconds()
      );
    },
    // ----------以下为导出Excel数据功能--------------
    exportToExcel() {
      //excel数据导出
      require.ensure([], () => {
        const { export_json_to_excel } = require("../../excel/Export2Excel");
        const tHeader = [
          "序号",
          "楼栋",
          "楼层",
          "房间",
          "设备类型",
          "设备编号",
          "设备名称",
          "抄表月份",
          "起码",
          "止码",
          "差额",
          "月用量",
        ];
        const filterVal = [
          "id",
          "houseNum",
          "floor",
          "rooms",
          "typeName",
          "typeCoding",
          "equipmentNumber",
          "month",
          "startNum",
          "endNum",
          "nums",
          "nums",
        ];
        const list = this.tableData;
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, "月抄表统计excel");
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) => filterVal.map((j) => v[j]));
    },
    //查看方法
    updatedata() {
      if (this.multipleSelection.length != 1) {
        this.$message.error("请选择数据");
        return;
      }
      this.dialogVisibleupdate = true;
      this.multipleSelection.forEach((res) => {
        this.ruleForms = res;
      });
    },
  },
};
</script>